package top.andoudou.generator.mybatis.config;

import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.toolkit.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 生成DTO与xml mapper
 */
public class InjectionConfigExt extends InjectionConfig implements ContextSetting{

    @Override
    public void init(CodeProperties properties) {
        this.globalConfig = properties.getGlobal();
        this.packageConfig = properties.getPackages();

        this.init();
    }

    @Override
    public InjectionConfig getInjectionConfig() {
        return this;
    }

    /**
     * 注入自定义 Map 对象
     */
    @Override
    public void initMap() {

    }

    GlobalConfig globalConfig;

    PackageConfig packageConfig;

    List<FileOutConfig> focList = new ArrayList<FileOutConfig>();

    public InjectionConfigExt(){

    }



    protected void init(){
        if(focList.isEmpty()) {
            initList();
        }
    }

    private void initList() {
        //DTO生成
        this.focList.add(dto());

        this.focList.add(adddto());
        this.focList.add(updateDTO());
        //生成xml
        this.focList.add(xml());

        this.setFileOutConfigList(focList);
    }

//    生成DTO
    private FileOutConfig dto(){

        return  new FileOutConfig("/templates/template_entity_dto.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                String path = joinPath(packageConfig.getParent(),packageConfig.getEntity()+".dto."+tableInfo.getEntityName().toLowerCase());
                path =  globalConfig.getOutputDir()+path;
                File file = new File(path);
                if(!file.exists()){
                    file.mkdirs();
                }

                return new File(file,tableInfo.getEntityName() + "Dto.java").getAbsolutePath();
            }
        };
    }

    //    生成ADDDTO
    private FileOutConfig adddto(){

        return  new FileOutConfig("/templates/template_entity_add_dto.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                String path = joinPath(packageConfig.getParent(),packageConfig.getEntity()+".dto."+tableInfo.getEntityName().toLowerCase());
                path =  globalConfig.getOutputDir()+path;
                File file = new File(path);
                if(!file.exists()){
                    file.mkdirs();
                }

                return new File(file,tableInfo.getEntityName() + "AddDto.java").getAbsolutePath();
            }
        };
    }

    //    生成updateDTO
    private FileOutConfig updateDTO(){

        return  new FileOutConfig("/templates/template_entity_update_dto.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                String path = joinPath(packageConfig.getParent(),packageConfig.getEntity()+".dto."+tableInfo.getEntityName().toLowerCase());
                path =  globalConfig.getOutputDir()+path;
                File file = new File(path);
                if(!file.exists()){
                    file.mkdirs();
                }

                return new File(file,tableInfo.getEntityName() + "UpdateDto.java").getAbsolutePath();
            }
        };
    }


    private FileOutConfig xml() {
        return new FileOutConfig("templates/template_mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return globalConfig.getOutputDir() + "mapper" + File.separator + tableInfo.getXmlName() + ".xml";
            }
        };
    }


    private String joinPath(String parentDir, String packageName) {
        if (StringUtils.isEmpty(parentDir)) {
            parentDir = System.getProperty(ConstVal.JAVA_TMPDIR);
        }
        if (!StringUtils.endsWith(parentDir, File.separator)) {
            parentDir += File.separator;
        }

        String answer = parentDir+ packageName;
        answer = answer.replaceAll("\\.", "\\" + File.separator);

        return answer;
    }

    public GlobalConfig getGlobalConfig() {
        return globalConfig;
    }

    public void setGlobalConfig(GlobalConfig globalConfig) {
        this.globalConfig = globalConfig;
    }

    public PackageConfig getPackageConfig() {
        return packageConfig;
    }

    public void setPackageConfig(PackageConfig packageConfig) {
        this.packageConfig = packageConfig;
    }



}
